package com.itheima.java.ai.langchin4j.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.java.ai.langchin4j.entity.DoctorSchedule;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface DoctorScheduleMapper extends BaseMapper<DoctorSchedule> {
    
    @Select("SELECT * FROM doctor_schedules WHERE doctor_id = #{doctorId} AND work_date = #{workDate} ORDER BY time_slot")
    List<DoctorSchedule> findByDoctorIdAndDate(Long doctorId, LocalDate workDate);
    
    @Select("SELECT * FROM doctor_schedules WHERE doctor_id = #{doctorId} AND work_date >= #{startDate} AND work_date <= #{endDate} ORDER BY work_date, time_slot")
    List<DoctorSchedule> findByDoctorIdAndDateRange(Long doctorId, LocalDate startDate, LocalDate endDate);
    
    @Select("SELECT * FROM doctor_schedules WHERE work_date = #{workDate} AND available_slots > 0 ORDER BY doctor_id, time_slot")
    List<DoctorSchedule> findAvailableByDate(LocalDate workDate);
    
    @Update("UPDATE doctor_schedules SET booked_slots = booked_slots + 1, available_slots = available_slots - 1 WHERE id = #{scheduleId} AND available_slots > 0")
    int bookSlot(Long scheduleId);
    
    @Update("UPDATE doctor_schedules SET booked_slots = booked_slots - 1, available_slots = available_slots + 1 WHERE id = #{scheduleId}")
    int cancelSlot(Long scheduleId);
}